import { _decorator, Asset, AudioClip, AudioSource, Component, director, error, Node } from 'cc';
const { ccclass, property } = _decorator;

export class SpecialResMgr{
    _mapRes:Map<string,Asset> = new Map();
    constructor(){

    }
    //存储
    addRes(resname:string,res:Asset){
        if(resname.length<=0||!res){
            error("名字为空或资源不存在")
            return;
        }
        if(this._mapRes.has(resname)){
            return;
        }
        this._mapRes.set(resname,res);

    }
    //读取
    getRes<T>(resname:string):T{
        if(resname.length<=0){
            error("名字为空")
            return;
        }
        return this._mapRes.get(resname) as T;

    }
}
export class PrefabMgr extends SpecialResMgr{
    constructor(){
        super();
    }
}
export class SpriteFrameMgr extends SpecialResMgr{
    constructor(){
        super();
    }
}
export class AudioClipMgr extends SpecialResMgr{
    private audioBgmSourece:AudioSource=null;
    private audioEffectSourece:AudioSource=null;
    constructor(){
        super();
        this.init();
    }
    private init(){
        let audioBmgNode = new Node();
        this.audioBgmSourece = audioBmgNode.addComponent(AudioSource);
        audioBmgNode.parent =director.getScene();       

        let audioEffectNode = new Node();
        this.audioEffectSourece = audioEffectNode.addComponent(AudioSource);
        audioEffectNode.parent =director.getScene();
        
        director.addPersistRootNode(audioBmgNode);
        director.addPersistRootNode(audioEffectNode);
    }
    playbgm(name:string,loop:boolean=false,volume:1){
        let clip = this.getRes<AudioClip>(name);
        this.audioBgmSourece.clip=clip;
        this.audioBgmSourece.loop=loop;
        this.audioBgmSourece.volume=volume;
        this.audioBgmSourece.play();
    }
    playeffect(name:string,loop:boolean=false,volume:1){
        let clip = this.getRes<AudioClip>(name);
        this.audioEffectSourece.clip=clip;
        this.audioEffectSourece.loop=loop;
        this.audioEffectSourece.volume=volume;
        this.audioEffectSourece.play();
    }
    
}



